/**
* 重置密码的判断操作 author:PH
*/
$(function () {
console.log("hhh");
    console.log(getQueryVariable("code"));
    var code=getQueryVariable("code")
    if(code==false){
        console.log("login");
    }else{
        console.log("reset");
        console.log(queryCodeTime(code));
    }
    Check();
});
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return false;
}
function queryCodeTime(code){
    $.ajax({
        type : "get",
        url : "/user_control/queryCodeTime",
        data : {
            code : code,
        },
        async : false,
        success : function(data){
            if(data.status=="success"){
                var outdate= data.rows.outdate;
                var d = new Date(outdate);
                var nd = new Date();
                console.log("nd.getTime():"+nd.getTime());
                console.log("d.getTime():"+d.getTime());
                if(nd.getTime()>d.getTime()){
                    console.log("已超时");
                    Alert.alert("链接已失效");
                    setTimeout(function () { window.location.href = "/index.html"; }, 5000);
                }else{
                    reset_pwd();
                    console.log("未超时");
                }
            }else{
                window.location.href = "/index.html";
            }
        }
    });
}

function checkReset() {
    var username = $('#login-username').val()
    if (username == null || username == '') {
        Alert.alert('用户名不能为空！');
        return false;
    }
    $.ajax({
        type : "post",
        url : "/user_control/isUser",
        data : {
            username : username,
        },
        async : false,
        success : function(data){
        if (data.status == "success") {
            //console.log(data);
            old_pwd = data.rows.password;
            email=data.rows.email;
            if (email== "" || email == null){
                Alert.alert('用户邮箱地址不存在！');
                return;
            };
            var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            if (!myreg.test(email)){
                Alert.alert('用户邮箱格式不正确！');
                return;
            };
            Alert.alert('请登录到邮箱重置密码');
            sendMail(username,email);



            //window.location.href = "/index.html";

        } else {
            Alert.alert(data.msg);
            return;
        }
    }//success
    });
    return true;
}

function reset_pwd(){
    console.log("function reset_pwd");
    $('#reset_user_name').val("");
    $('#new_pwd').val("");
    $('#new_pwd_confirm').val("");
    $('#reset_pwd').modal();
}
function Check(){
    console.log("function Check");
    $('#reset_pwd_submit').on('click', function () {
        var username = $('#reset_user_name').val();
        var new_pwd = $('#new_pwd').val();
        var new_pwd_confirm = $('#new_pwd_confirm').val();
        var old_pwd = "";

        if (username == "" || username == null){
            Alert.alert('请先输入用户名！');
            return;
        }else{
            $.ajax({
                type : "post",
                url : "/user_control/isUser",
                data : {
                    username : username,
                },
                async : false,
                success : function(data) {
                    if (data.status == "success") {
                        //console.log(data);
                        old_pwd = data.rows.password;
                        if (new_pwd == "" || new_pwd == null){
                            Alert.alert('请先输入新密码！');
                            return;
                        };
                        if (new_pwd == old_pwd){
                            Alert.alert('新密码不能和旧密码相同！');
                            return;
                        };
                        if (new_pwd.length<6){
                            Alert.alert('密码长度不能小于6位！');
                            return;
                        };
                        if (new_pwd_confirm != new_pwd){
                            Alert.alert('密码不一致！');
                            return;
                        };
                        console.log("username:"+username);
                        update_new_pwd(username,new_pwd);

                    } else {
                        Alert.alert(data.msg);
                        return;
                    }
                }//success
//                error : function(){
//                    Alert.alert("通讯异常");
//                }//error
            }); //ajax
        }//else
    });
}//Check()

function sendMail(to_username,to_email){
    //console.log("confirm!");
    $.ajax({
        type : "post",
        url : "/user_control/sendEmail",
        data : {
            username : to_username,
            email:to_email
        },
        async : false,
        success : function(data) {
            console.log("send Email:"+data);
        }
    });
}

function update_new_pwd(username,new_pwd){
    $.ajax({
        type:"post",
        url:"/user_control/update_new_pwd",
        data: {
            username : username,
            password: new_pwd
        },
        async : false,
        success : function(data) {
            //console.log(data);
            Alert.alert(data.msg);
            setTimeout(function () { window.location.href = "/index.html"; }, 5000);
        }
    });
}
